package jsonTest;

import java.io.IOException;

import json.Json_Account;
import json.Json_City;
import json.Json_Company;
import json.Json_Product;
import json.Json_Result;
import jsonTest.JsonUtil;

import handler.Handler;
import handler.Message;

import org.json.JSONException;

import util.LogUtil;

import com.ruiyi.baseparser.Group;
import com.ruiyi.error.RuiyiCredentialsException;
import com.ruiyi.error.RuiyiError;
import com.ruiyi.error.RuiyiException;
import com.ruiyi.error.RuiyiParseException;


public class _Test {
    private static final String TAG = "Main\t\t";

    static Json_Account ac;
    public static void main(String[] argz) {
////    	testCities();
//    	
////    	testProductList();
//    	
////    	testLogin();
////    	
//    	
////    	String s = "";
////    	testGetCompanyByUrl(s);
    	
//    	String qr_code = "https://www.163.com";
//    	if(qr_code.subSequence(0, 7).equals("http://") || 
// 			   qr_code.subSequence(0, 8).equals("https://"))
//    		LogUtil.d(TAG, "is url");
//    	else
//    		LogUtil.d(TAG, "not url");
    	
    	MyCompany mycompany = new MyCompany();
    	mycompany.login("locoso@locoso.com", "locoso", null);
    }

	private static void testGetCompanyByUrl(String s) {
		// TODO Auto-generated method stub
		
	}

	private static void testProductList() {
		JsonUtil util = new JsonUtil();
		try {
			Group<Json_Product> products = util.getProductList(4303825);
			for(Json_Product tmp : products)
				LogUtil.d(TAG, tmp.getProductName());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void testCities() {
    	JsonUtil util = new JsonUtil();
    	
    	Object obj = null;
    	try {
//    		//	***********************************************************************************
//    		//test Read interface
//    		long city_id = 10933;
//    		Json_City city = util.city(city_id);
//    		LogUtil.d(TAG, city.getName());
//    		
//    		long cities_id = 10884;
//			Group<Json_City> cities = util.cities(cities_id);
//			for(Json_City tmp : cities)
//			LogUtil.d(TAG, tmp.getName());
    		
    		//	***********************************************************************************
    		//test Manage interface
    		obj = util.login("locoso@locoso.com", "locoso");
    		
    		if(obj instanceof Json_Account){
        		Json_Account ac = (Json_Account)obj;
        		LogUtil.d(TAG, ac.getSession());
        		
        		Object ob = util.getMyCompany(ac);
        		if(obj instanceof Json_Company)
        		LogUtil.d(TAG, ((Json_Company)ob).getCompanyName());
        		return;
    		}
    		
    		if(obj instanceof Json_Result){
    			Json_Result result = (Json_Result) obj;
    			LogUtil.d(TAG, "result.getMessage():"+result.getMessage());
    		}
    		
		} catch (RuiyiCredentialsException e) {
			e.printStackTrace();
		} catch (RuiyiError e) {
			e.printStackTrace();
		} catch (RuiyiException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	private static void testLogin(){
		Mgr.getInstance().login("locoso@locoso.com", "locoso", new Mgr.OnDataReady(){
			@Override
			public void setData(Object obj) {
				if(obj == null){
					LogUtil.e(TAG, "obj null");
					return;
				}
				if(obj instanceof Json_Account){
					ac = (Json_Account)obj;
					LogUtil.e(TAG, "obj not null.\tsession:" + ac.getSession());	
					return;
				}
				if(obj instanceof Json_Result){
					LogUtil.e(TAG, "obj not null.\tmessage:" + ((Json_Result)obj).getMessage());	
					return;
				}			
			}
		});
	}
}